package com.ctl.juc;

import java.util.concurrent.*;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.stream.IntStream;

/**
 * <p>Title: LongAccumulatorTest</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: www.ctl.com</p>
 *
 * @author ctl
 * @version 1.1
 * @date 2023-02-27 14:16
 */
public class LongAccumulatorTest {

    static ExecutorService executor = new ThreadPoolExecutor(5, 10, 1000, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));

    public static void main(String[] args) throws InterruptedException {
        LongAccumulator accumulator = new LongAccumulator((x, y) -> x + y, 0);
        IntStream.range(1, 10).forEach(i -> executor.submit(() -> accumulator.accumulate(i)));
        Thread.sleep(2000);
        System.out.println(accumulator.getThenReset());

        LongAccumulator accumulator2 = new LongAccumulator((x, y) -> (x * y) + (x + y), 0);
        IntStream.range(1, 10).forEach(i -> executor.submit(() -> {
            accumulator2.accumulate(i);
            System.out.println(accumulator2.get());
        }));
        Thread.sleep(2000);
        executor.shutdown();
    }
}
